package com.sxz.sbs.module.order;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.order.mode.IntentOrder;
import com.sxz.sbs.module.order.presenter.AllotOrderPresenter;
import com.sxz.sbs.module.order.view.IAllotOrderView;
import com.sxz.sbs.module.staff.OperateSuccessActivity;
import com.sxz.sbs.module.staff.adapter.StaffDisposeAdapter;
import com.sxz.sbs.module.staff.mode.StaffDisposeBean;
import com.sxz.sbs.widget.CommonTitle;
import com.sxz.sbs.widget.StallAlertDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 分配订单
 *
 * @author tian
 */
public class AllotOrderActivity extends BaseActivity<AllotOrderPresenter> implements IAllotOrderView {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private List<StaffDisposeBean.ListBean> dataList = new ArrayList<>();
    private StaffDisposeAdapter disposeAdapter;
    private int currentPage = 1;
    private boolean isList = true;
    private IntentOrder intentOrder;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("分配订单").build().show();
        intentOrder = getIntent().getParcelableExtra("intent_order");
        if (intentOrder == null) {
            ToastUtils.showShort("请求出错，请稍后再试");
            finish();
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getStaff();
            }
        });
        refreshLayout.setRefreshing(true);
        getStaff();
        disposeAdapter = new StaffDisposeAdapter(StaffDisposeAdapter.DisposeEnum.ALLOT_ORDER,dataList);
        recyclerView.setAdapter(disposeAdapter);
        disposeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getStaff();
            }
        },recyclerView);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, final int position) {
                new StallAlertDialog.Builder(AllotOrderActivity.this)
                        .setMessage("确定要把订单分配给此员工吗")
                        .setPositiveButton(new StallAlertDialog.OnPositiveListener() {
                            @Override
                            public void onPositiveClick(View view) {
                                allotOrder(dataList.get(position).getId());
                            }
                        }).create().show();
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getStaff();
            }
        });
    }
    /**
     * 获取员工列表
     */
    private void getStaff() {
        isList = true;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("parkingId",intentOrder.getParkingId());
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getStaffByPId(map);

    }

    /**
     * 分配订单
     */
    private void allotOrder(int staffId) {
        isList = false;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("orderId",intentOrder.getOrderId());
        map.put("managerId", staffId);
        mPresenter.allotOrder(map);

    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_allot_order;
    }

    @Override
    protected AllotOrderPresenter createPresenter() {
        return new AllotOrderPresenter();
    }


    @Override
    public void onGetStaffSuccess(StaffDisposeBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            if (bean.getList() != null && bean.getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    disposeAdapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(bean.getList());
                    disposeAdapter.notifyDataSetChanged();
                }
                disposeAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    disposeAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        disposeAdapter.loadMoreEnd();
                    } else {
                        disposeAdapter.loadMoreEnd(true);
                    }
                }
            }

        }
    }

    @Override
    public void onAllotOrderSuccess() {
        startActivity(OperateSuccessActivity.class);
        finish();
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (isList) {
            if (currentPage == 1) {
                if (errorTag == AppConstant.NET_ERROR_CODE) {
                    stateLayout.showNetwork();
                } else {
                    stateLayout.showEmpty();
                }
            } else {
                disposeAdapter.loadMoreFail();
            }
        } else {
            super.showError(errorTag,msg);
        }
    }
}
